#include "monthassessmentview.h"
#include <QHeaderView>
#include <QMessageBox>
#include <QDebug>

MonthAssessmentView::MonthAssessmentView(QWidget *parent) : QWidget(parent),
    m_adminManager(nullptr),
    m_assessmentTable(nullptr),
    m_monthComboBox(nullptr),
    m_queryButton(nullptr)
{
    m_adminManager = new AdminManager(this);
    initUI();
}

void MonthAssessmentView::initUI()
{
    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 创建查询区域布局
    QHBoxLayout *queryLayout = new QHBoxLayout();

    // 添加部门选择
    QLabel *deptLabel = new QLabel("选择部门:", this);
    m_deptComboBox = new QComboBox(this);
    m_deptComboBox->addItem("所有部门");

    // 添加月份选择
    QLabel *monthLabel = new QLabel("选择月份:", this);
    m_monthComboBox = new QComboBox(this);
    m_monthComboBox->addItem("全部");
    // 添加近12个月的选项
    QDate currentDate = QDate::currentDate();
    for (int i = 0; i < 12; ++i) {
        QDate date = currentDate.addMonths(-i);
        m_monthComboBox->addItem(date.toString("yyyy-MM"));
    }

    // 添加查询按钮
    m_queryButton = new QPushButton("查询", this);

    // 添加到查询布局
    queryLayout->addWidget(deptLabel);
    queryLayout->addWidget(m_deptComboBox);
    queryLayout->addWidget(monthLabel);
    queryLayout->addWidget(m_monthComboBox);
    queryLayout->addWidget(m_queryButton);
    queryLayout->addStretch();

    // 创建考核表格
    m_assessmentTable = new QTableWidget(this);
    m_assessmentTable->setColumnCount(10);
    QStringList headers;
    headers << "员工ID" << "员工姓名" << "部门ID" << "部门名称" << "迟到次数" << "请假次数" << "早退次数" << "迟到且早退次数" << "缺勤次数" << "考核分数";
    m_assessmentTable->setHorizontalHeaderLabels(headers);
    m_assessmentTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    m_assessmentTable->setEditTriggers(QAbstractItemView::NoEditTriggers);

    // 添加到主布局
    mainLayout->addLayout(queryLayout);
    mainLayout->addWidget(m_assessmentTable);

    // 设置布局
    setLayout(mainLayout);

    // 连接信号槽
    connect(m_queryButton, &QPushButton::clicked, this, &MonthAssessmentView::onQueryButtonClicked);

    // 默认显示全部数据
    onQueryButtonClicked();
}

void MonthAssessmentView::setAdminManager(AdminManager *manager)
{
    m_adminManager = manager;
    if (m_adminManager) {
        refreshDepartmentList();
    }
}

void MonthAssessmentView::refreshDepartmentList()
{
    if (!m_adminManager) return;

    m_deptComboBox->clear();
    m_deptComboBox->addItem("所有部门", -1);

    QList<QVariantList> departments = m_adminManager->getAllDepartments();
    for (const auto &deptData : departments) {
        int deptId = deptData[0].toInt();
        QString deptName = deptData[1].toString();
        m_deptComboBox->addItem(deptName, deptId);
    }
}

void MonthAssessmentView::refreshAssessmentData()
{
    if (!m_adminManager) {
        QMessageBox::warning(this, "警告", "未设置管理员管理器");
        return;
    }

    // 获取选中的月份和部门
    QString selectedMonth = m_monthComboBox->currentText();
    int selectedDeptId = m_deptComboBox->currentData().toInt();

    // 如果选择"全部"，则传入空字符串
    if (selectedMonth == "全部") {
        selectedMonth = "";
    }

    // 从数据库获取考核数据
    QList<QVariantList> assessmentData = m_adminManager->getAllEmployeesMonthlyAssessment(selectedMonth, selectedDeptId);

    // 清空表格
    m_assessmentTable->setRowCount(0);

    // 填充表格
    for (int i = 0; i < assessmentData.size(); ++i) {
        const QVariantList &rowData = assessmentData[i];
        m_assessmentTable->insertRow(i);

        for (int j = 0; j < rowData.size() && j < m_assessmentTable->columnCount(); ++j) {
            QTableWidgetItem *item = new QTableWidgetItem(rowData[j].toString());
            m_assessmentTable->setItem(i, j, item);
        }
    }
}

void MonthAssessmentView::onQueryButtonClicked()
{
    refreshAssessmentData();
}
